<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 设备预警 </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'management:inFireAlarmInfo:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.name }}
        </a>
      </template>
      <!-- 新增 action 插槽 -->
      <template #action="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'management:inFireAlarmInfo:edit'" type="text">
        <span style="font-size: 14px; color: #4169E1;">详情</span>
        </a-button>
        <Popconfirm
          title="是否确认删除设备预警表"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
          @cancel="handleCancel"
        >
          <a-button 
            v-if="record.status == '9'" 
            v-auth="'management:inFireAlarmInfo:edit'" 
            type="text" 
            style="color: red;font-size:14px;"
          >
            删除
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup name="ViewsManagementInFireAlarmInfoList">
import { unref, ref, computed, onMounted } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
import { Icon } from '/@/components/Icon';
import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
import { inFireAlarmInfoDelete, inFireAlarmInfoListData } from '/@/api/management/inFireAlarmInfo';
import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
import { useDrawer } from '/@/components/Drawer';
import { useModal } from '/@/components/Modal';
import { FormProps } from '/@/components/Form';
import { officeTreeData } from '/@/api/sys/office';
import { unitRegionListData } from '/@/api/unitmanage/unitRegion';
import { inDeviceManagementListData } from '/@/api/device.management/inDeviceManagement';
import InputForm from './form.vue';
import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件

const { t } = useI18n('management.inFireAlarmInfo');
const { showMessage } = useMessage();
const { meta } = unref(router.currentRoute);
const route = router.currentRoute.value;

const getTitle = {
  icon: meta.icon || 'i-ant-design:book-outlined',
  value: meta.title || t('设备预警表管理'),
};

const regionName = ref('');
const regionParams = computed(() => {
  return {
    name: regionName.value,
  };
});

const inFireAlarmInfoName = ref('');

const inFireAlarmInfoParams = computed(() => {
  return {
    name: inFireAlarmInfoName.value
  }
});

const EnterpriseParams = ref<Recordable>({
  name: ''
});

const equipmentParams = ref({
  equipmentName: ""
});

const searchForm: FormProps = {
  baseColProps: { md: 8, lg: 6 },
  labelWidth: 90,
  schemas: [
    {
      label: t(''),
      field: 'id',
      fieldLabel: 'name',
      component: 'TreeSelect',
      componentProps: {
        placeholder: t('请输入设备预警名称'),
        api: inFireAlarmInfoListData,
        resultField: 'list',
        fieldNames: { label: 'name', value: 'id' },
        params: inFireAlarmInfoParams,
        allowClear: true,
        immediate: false,
        each: false,
        showSelect: true,
        optionFilterProp: 'label',
        showSearch: true,
        onSearch: (value) => {
          inFireAlarmInfoName.value = value;
          reload()
        },
      }
    },
    {
      label: t(''),
      field: 'enterpriseId',
      fieldLabel: 'enterpriseName',
      component: 'TreeSelect',
      componentProps: () => {
        return {
          placeholder: t('请输入所属主体'),
          api: unitEnterpriseDominantListData,
          params: EnterpriseParams.value,
          fieldNames: { label: 'name', value: 'unitCode' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            EnterpriseParams.value.name = value;
            console.log(EnterpriseParams.value);
          },
        };
      },
    },
    {
      label: t(''),
      field: 'regionId',
      fieldLabel: 'regionInName',
      component: 'TreeSelect',
      componentProps: {
        placeholder: t('请输入区域名称'),
        api: unitRegionListData,
        resultField: 'list',
        fieldNames: { label: 'name', value: 'id' },
        params: regionParams,
        allowClear: true,
        immediate: false,
        each: false,
        showSelect: true,
        optionFilterProp: 'label',
        showSearch: true,
        onSearch: (value) => {
          regionName.value = value;
          reload()
        },
      }
    },
    {
      label: t(''),
      field: 'equipmentId',
      fieldLabel: 'equipmentName',
      component: 'Select', 
      componentProps: () => {
        return {
          placeholder: t('请输设备名称'),
          api: inDeviceManagementListData,
          params: equipmentParams.value,
          fieldNames: { label: 'equipmentName', value: 'equipmentCode' },
          resultField: 'list',
          notFoundContent: '未查询到记录',
          filterTreeNode: false,
          allowClear: true,
          filterOption: false,
          immediate: true,
          onSearch: (value: string) => {
            equipmentParams.value.equipmentName = value;
            console.log(equipmentParams.value);
          },
        };
      },
    },
  ],
};

const tableColumns: BasicColumn[] = [
  {
    title: t('所属主体'),
    dataIndex: 'enterpriseName',
    key: 'a.enterprise_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('火情预警名称'),
    dataIndex: 'name',
    key: 'a.name',
    sorter: true,
    width: 230,
    align: 'left',
    slot: 'firstColumn',
  },
  {
    title: t('设备名称'),
    dataIndex: 'equipmentName',
    key: 'a.equipment_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('预警时间'),
    dataIndex: 'alarmTime',
    key: 'a.alarm_time',
    sorter: true,
    width: 130,
    align: 'center',
  },
  {
    title: t('反馈内容'),
    dataIndex: 'feedback',
    key: 'a.feedback',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('建筑名称'),
    dataIndex: 'buildName',
    key: 'a.build_name',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('区域名称'),
    dataIndex: 'regionIname',
    key: 'a.region_iname',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('预警信息'),
    dataIndex: 'alarmInfo',
    key: 'a.alarm_info',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('处置状态'),
    dataIndex: 'state',
    key: 'a.state',
    sorter: true,
    width: 130,
    align: 'left',
  },
  {
    title: t('操作'),
    dataIndex: 'action',
    key: 'action',
    width: 160,
    fixed: 'right',
    slot: 'action'
  }
];

const [registerTable, { reload }] = useTable({
  api: inFireAlarmInfoListData,
  beforeFetch: (params) => {
    const urlParams = new URLSearchParams(route.fullPath.split('?')[1]);
    const tidFromUrl = urlParams.get('tid');
    if (tidFromUrl) {
      params.tid = decodeURIComponent(tidFromUrl);
    }
    return params;
  },
  columns: tableColumns,
  formConfig: searchForm,
  showTableSetting: true,
  useSearchForm: true,
  canResize: true,
});

const [registerDrawer, { openDrawer }] = useDrawer();

function handleForm(record: Recordable) {
  openDrawer(true, record);
}

async function handleDelete(record: Recordable) {
  const params = { tid: record.tid };
  const res = await inFireAlarmInfoDelete(params);
  showMessage(res.message);
  handleSuccess(record);
}

function handleSuccess(record: Recordable) {
  reload({ record });
}

const [registerTraceModal, { openModal: traceModel }] = useModal();

function handleTrace(record: Recordable) {
  traceModel(true, { formKey: 'equipment_fire_warning_process', bizKey: record.taskInfoId });
}

function handleCancel() {
  console.log('取消删除操作');
}

onMounted(() => {
  const { tid, openDrawer: shouldOpenDrawer } = route.query;
  if (shouldOpenDrawer === 'true' && tid) {
    handleForm({ tid });
  }
});
</script>    